<?php

namespace MLM\Model\Commission;


use MLM\Model\Commission;

class Storage extends \MLM\Model\Generic\Db\Storage {

    
    public function getTotalCommission($memberId, $transType) {

        $data = array();


        IF ($transType == 'REF'){
            $sql = "select IFNULL(sum(comm),0) as total from (
                    SELECT 0.15 * hi.subs_amount comm
                    FROM ( SELECT CPriorSponsor(member_id) AS member_id, @level AS level
                    FROM ( SELECT @start_with := ? ,@id := @start_with,@level := 0 ) vars, members
                    WHERE @id IS NOT NULL ) ho JOIN members hi ON hi.member_id = ho.member_id
                    ) xx
                     ";
        }ELSE IF ($transType == 'Q203'){
            
            $sql = "select IFNULL(sum(total_bonus * 400 * 0.1),0) as total from
                    t_bonus_pasang WHERE member_id = ? ";
        }
        
        if ($searchField != ""){
            $sql .= " and $searchField ";
        }

        $stmt = $this->_db->query($sql);
        $getTotal = $stmt->execute(array($memberId));

        $total = 0;
        while($getTotal->next()){
                $res = $getTotal->current();
                $total = $res['total'];

        }

        return $total;

    }

}
